package Stack;

public class Test01 {
    // 例题3.1
    public static void isMatched(SqStack sqStack, String str)throws Exception{
        for(int i=0; i<str.length(); i++){
            if(str.charAt(i)== '('){
                sqStack.push('(');
            }else if(str.charAt(i)==')' && !sqStack.isEmpty()){
                // 符合要求抛出栈顶
                sqStack.pop();
            }else if(str.charAt(i)==')' && sqStack.isEmpty()){
                // 有 右括号没有左括号
                System.out.println("括号不匹配");
                return;
            }
        }
        //如果最后的结果栈不为空 代表左括号多了
        if(sqStack.isEmpty()){
            System.out.println("括号匹配");
        }else {
            System.out.println("括号不匹配");
        }
    }

    public static void main(String[] args) throws Exception {
        String str1 = "(a+b*(c+d))";
        String str2 = "(a+b*(c+d)))";
        SqStack q = new SqStack(str1.length());
        Test01.isMatched(q, str1);

        SqStack p = new SqStack(str2.length());
        Test01.isMatched(p, str2);

    }

}
